<?php

namespace seecms\controller;

use magein\utils\Result;
use seecms\See;
use seecms\SeeLang;

class SeeBaseController
{

    protected $view = null;

    public function __construct()
    {
        $this->view = See::view();
        $this->init();
    }

    protected function init()
    {

    }

    /**
     * 赋值
     * @param $key
     * @param $vars
     * @return void
     */
    protected function assign($key, $vars)
    {
        $this->view->assign($key, $vars);
    }

    /**
     * @param $file
     * @param array $assign
     * @return string
     */
    protected function view($file, array $assign = []): string
    {
        return $this->view->fetch($file, $assign);
    }

    /**
     * @param $data
     * @param string|null $message
     * @param int $code
     * @return array
     */
    protected function success($data = null, string $message = null, int $code = 0): array
    {
        return Result::success($data, $message, $code)->toArray();
    }

    /**
     * @param string|null $message
     * @param int $code
     * @param $data
     * @return array
     */
    protected function fail(string $message = null, int $code = 1, $data = null): array
    {
        return Result::error($message, $code, $data)->toArray();
    }

    /**
     * @param $data
     * @param int $code
     * @return array
     */
    public function saveSuccess($data = null, int $code = 0): array
    {
        return $this->success($data, SeeLang::operate('save_success') ?: 'save success', $code);
    }

    /**
     * @param int $code
     * @param null $data
     * @return array
     */
    public function saveFail(int $code = 1, $data = null): array
    {
        return $this->fail(SeeLang::operate('save_fail') ?: 'save fail', $code, $data);
    }
}